/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        permissions:['skill:add','skill:update'],//本页权限数组
        content:'',
        desc:'',
        classList:[],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getClassList();
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);

            return ;
        }

        if(!empty(aGET.id)){
            that.getInfoById(aGET.id);
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 得到班级
        getClassList: function() {
            var that = this;
            Get(that, _HOST + _API.organization_courseClass_getClassBy + "?schoolId="+schoolId+"&companyId="+companyId)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        that.classList = response.list;
                    }
                });
        },
        getInfoById:function (id) {
            var that = this;
            Get(that,_HOST+_API.weChat_courseSeKill_getById+"?id="+id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            var list = response.data;
                            for (var item in list) {
                                $("#Form input[name='" + item + "']").val(list[item]);
                            }
                            setTimeout(function () {
                                $("#Form select[name='classId']").val(list.classId);
                            },1000)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 添加
        add:function () {
            var that = this;
            var param = {};
            var flg = 0;
            $("#Form").find('input,textarea,select').each(function(){
                var paramName = $(this).attr('name');
                if (paramName == 'classId' && empty($(this).val())) {
                    flg = 1;
                }
                if (paramName == 'stock' && empty($(this).val())) {
                    flg = 2
                }
                param[$(this).attr('name')] = $(this).val();
            });
            if (flg == 1) {
                layer.msg('请选择班级', {time: 1500, icon:5});
                return;
            } else if (flg == 2) {
                layer.msg('库存不能为空', {time: 1500, icon:5});
                return;
            }
            // console.log(param)
            Post(that,_HOST+_API.weChat_courseSeKill_add,param,true,that.permissions[0])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        // 修改
        update:function () {
            var that = this;
            var param = {};
            var flg = 0;
            $("#Form").find('input,textarea,select').each(function(){
                var paramName = $(this).attr('name');
                if (paramName == 'classId' && empty($(this).val())) {
                    flg = 1;
                }
                if (paramName == 'stock' && empty($(this).val())) {
                    flg = 2
                }
                param[$(this).attr('name')] = $(this).val();
            });
            if (flg == 1) {
                layer.msg('请选择班级', {time: 1500, icon:5});
                return;
            } else if (flg == 2) {
                layer.msg('库存不能为空', {time: 1500, icon:5});
                return;
            }
            // console.log(param)
            Post(that,_HOST+_API.weChat_courseSeKill_update,param,true,that.permissions[1])
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        submit: function () {
            var that = this;
            if(empty(aGET.id)){
                that.add();
            }else {
                that.update();
            }
        }
    },
});
